//
//  TMPhoneLoginController.m
//  国学商家管理
//
//  Created by mac on 2018/6/19.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMPhoneLoginController.h"
#import "SPToolSet.h"
//#import "TMApproveController.h"
//#import "TMApproveAuditController.h"
@interface TMPhoneLoginController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *identifyCodeTf;
@property (weak, nonatomic) IBOutlet UIButton *identifyBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (nonatomic, strong) NSTimer *countDownTimer;
@property (nonatomic, assign) NSInteger timestamp;
@end

@implementation TMPhoneLoginController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //[self.identifyBtn startWithKey:@"keys"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.isblind ? @"绑定手机号":@"快捷登录";
    self.loginBtn.layer.cornerRadius = 12.0f;
    self.loginBtn.layer.masksToBounds = YES;
    self.phoneTf.delegate = self;
    self.identifyCodeTf.delegate = self;
    [self.loginBtn setTitle:self.isblind ? @"立即绑定":@"立即登录" forState:UIControlStateNormal];

}

- (IBAction)identifyBtnClick:(UIButton *)sender {
   // [self createTimer];
    if (![SPToolSet isMobileNumber:self.phoneTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入正确的手机号码！"];
        return;
    }else{
        [self.identifyBtn startTimer:60 andKey:@"keys"];
        [TMUserManager verificationCodeSendWithDictionary:self.phoneTf.text];
    }
}
- (IBAction)loginBtnClick:(UIButton *)sender {
    if (self.phoneTf.text.length!= 11 || !self.identifyCodeTf.text) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入正确的电话或者验证码！"];
        return;
    }
    if (self.isblind == YES) {
        //TMAPI_blindPhone
        [TMUserManager blindPhoneWith:@{@"phone":self.phoneTf.text,@"code":self.identifyCodeTf.text,@"openid":self.openid} Successed:^(id respon) {
             UIWindow *window = [UIApplication sharedApplication].keyWindow;
            TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
            window.rootViewController = rootTabbar;
        }];
    }else{
        [TMUserManager loginWithPhone:self.phoneTf.text code:self.identifyCodeTf.text successed:^(id index){
            [TMUserManager displayRCConfings];
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
            window.rootViewController = rootTabbar;
        }];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.phoneTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.phoneTf.text.length >= 11) {
            self.phoneTf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isFirstResponder]) {
        [textField resignFirstResponder];
    }
    return YES;
}

@end
